<template>
	<view class="familyPage">
		<view v-if="familyList.length==0">
			<u-empty text="暂无成员信息" mode="data"></u-empty>
		</view>
		<view v-if="familyList.length>0">
			<u-card title="住户卡" :sub-title="familyItem.buildingName+'/'+familyItem.unitName+'/'+familyItem.roomName" v-for="(familyItem,index) in familyList"
			 :key="index" :showFoot="false">
				<view slot="body">
					<view class="memberItem" v-for="(memberItem,idx) in familyItem.members" :key="idx">
						<u-field :label="memberItem.ownerType" :value="memberItem.ownerName" :disabled="true">
							<u-button size="mini" slot="right" type="warning" v-if="memberItem.canUnBind" @click="unBind(memberItem.id)">解绑</u-button>
						</u-field>
					</view>
				</view>
			</u-card>
			<view>
				<u-modal v-model="showTips" title="解绑确认单" content="解绑确认后不可恢复,请确认后操作!" :show-cancel-button="true" @confirm="confirmUnbind"></u-modal>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				familyList: [], //家庭成员信息
				id: '', //绑定id
				showTips: false, //展示模态框
			}
		},
		onLoad() {
			this.getFamilyInfo();
		},
		methods: {
			/* 获取家庭成员信息 */
			getFamilyInfo() {
				// let currentCommunity = uni.getStorageSync('current_community');
        let ownerInfo = uni.getStorageSync('ownerInfo')
        if (!ownerInfo) {
          this.$u.toast('请先登录,再进行操作')
          return;
        }
				this.$u.get('/new/new/ownerRoom/user/' + ownerInfo.id).then(res => {
					if (res.code == 0) {
						this.familyList = res.data.map(item => {
						  if(item.members!=null) {
                item.members = item.members.map(member => {
                  switch (member.ownerType) {
                    case 'yz':
                      member.ownerType = '业主';
                      break;
                    case 'zh':
                      member.ownerType = '租户';
                      break;
                    case 'qs':
                      member.ownerType = '亲属';
                      break;
                    default:
                      member.ownerType = '其他';
                  }

                  return member;
                })
              }

							return item;
						})
					} else {
						this.familyList = [];
						this.$u.toast(res.msg)
					}
				})
			},
			/* 解绑 */
			unBind(id) {
				console.log('解绑的绑定id', id)
				this.id = id;
				this.showTips = true;
			},
			confirmUnbind() {
				this.$u.delete('/new/new/ownerRoom/' + this.id).then(res => {
					console.log('解绑信息', res)
					if (res.code == 0) {
						this.$u.toast('解绑成功');
						this.getFamilyInfo();
					} else {
						this.$u.toast(res.msg)
					}
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
